package com.controller.admin;

import com.entity.BlogType;
import com.service.BlogService;
import com.service.BlogTypeService;
import com.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  管理员博客类别Controller层
 *  @author Administrator
 */
@Controller
@RequestMapping("/admin/blogType")
public class BlogTypeAdminController {
    @Resource
    private BlogTypeService blogTypeService;

    @Resource
    private BlogService blogService;

    /**
     * 分页查询博客类别信息
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value="offset",required=false)int offset,@RequestParam(value="limit",required=false)Integer limit,HttpServletResponse response)throws Exception{
        Map<String,Object> map = new HashMap();
        limit=limit+offset;
        map.put("size", limit);
        map.put("start", offset);
        List<BlogType> blogTypeList=blogTypeService.list(map);
        Long total=blogTypeService.getTotal(map);
        JSONObject result=new JSONObject();
        JSONArray jsonArray= JSONArray.fromObject(blogTypeList);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 添加或者修改博客类别信息
     * @param blogType
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    public String save(BlogType blogType,HttpServletResponse response)throws Exception{
        int resultTotal=0;
        if(blogType.getId()==null){
            resultTotal=blogTypeService.add(blogType);
        }else{
            resultTotal=blogTypeService.update(blogType);
        }
        JSONObject result=new JSONObject();
        if(resultTotal>0){
            result.put("success", true);
        }else{
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 博客类别信息删除
     * @param ids
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(value="ids",required=false)String ids, HttpServletResponse response)throws Exception{
        String []idsStr=ids.split(",");
        JSONObject result=new JSONObject();
        for(int i=0;i<idsStr.length;i++){
            if(blogService.getBlogByTypeId(Integer.parseInt(idsStr[i]))>0){
                result.put("exist", "博客类别下有博客，不能删除！");
            }else{
                blogTypeService.delete(Integer.parseInt(idsStr[i]));
            }
        }
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }
}
